package com.jeeplus.test.jlnu.teachingArchive.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Lists;
import com.jeeplus.test.jlnu.teachingArchive.domain.entity.RecordsInfo;
import com.jeeplus.test.jlnu.teachingArchive.domain.vo.RecordsInfoVo;
import com.jeeplus.test.jlnu.teachingArchive.service.RecordsInfoService;
import com.jeeplus.test.tools.resp.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping(value = "/test/records")
public class RecordsInfoController {
    @Autowired
    private RecordsInfoService recordsInfoService;
    @PostMapping("/compose")
    public R saveAndLink(@RequestBody RecordsInfoVo recordsInfoVo) {
        return recordsInfoService.saveAndLinkInside(recordsInfoVo);
    }
    @GetMapping("/detail/{id}")
    public R getRecordsDetail(@PathVariable String id) {
        RecordsInfoVo detailVo = recordsInfoService.getDetailById(id);
        return R.suc(detailVo);
    }
    @PostMapping("/saveOrUpdate")
    public RecordsInfoVo saveOrUpdate(@RequestBody RecordsInfoVo recordsInfoVo){
        return recordsInfoService.saveOrUpdate(recordsInfoVo);
    }
    @GetMapping("/page")
    public IPage<RecordsInfoVo> page(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            RecordsInfo query){
        Page<RecordsInfo> page = new Page<>(pageNum,pageSize);
        return recordsInfoService.page(page,query);
    }
    @DeleteMapping("/delete")
    public ResponseEntity<String> delete(@RequestParam(required = false) String ids) {
        if (ids == null || ids.trim().isEmpty()) {
            return ResponseEntity.badRequest().body("400: ids参数不能为空");
        }
        String[] idArray = ids.split(",");
        if (idArray.length == 0) {
            return ResponseEntity.badRequest().body("400: 未找到有效ID");
        }
        recordsInfoService.removeByIds(Lists.newArrayList(idArray));
        return ResponseEntity.ok("200");
    }
}
